package com.itheima.HomeWork.Demo02;

public class Demo01 {
    public static void main(String[] args) {
        MyThread myThread=new MyThread("售票口1");
        MyThread myThread1=new MyThread("售票口2");
        MyThread myThread2=new MyThread("售票口3");

        myThread.start();
        myThread1.start();
        myThread2.start();
    }
}
class MyThread extends Thread{

    static int ticket=100;
    MyThread(String name){
       super(name);
    }

    @Override
    public void run() {
        while(true){
            if(!sell()){
                System.out.println(getName()+"卖光了");
                break;
            }
            try {
                sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }
    private static synchronized boolean sell(){
        if(ticket<=0){

            return false;
        }else{
            ticket--;
            System.out.println(Thread.currentThread().getName()+"卖了一张票还剩"+ticket+"张");
            return true;
        }
    }



}
